﻿using System.Configuration;
namespace Framework
{
    /// <summary>
    /// 连接字符串读取
    /// </summary>
    public class PubConstant
    {
        static PubConstant()
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
            string conStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (conStringEncrypt == "true")
            {
                connectionString = DataEncryption.Decrypt(connectionString, ConnStringEncryptionKey);
            }
            _connectionString = connectionString;
        }
        private static readonly string ConnStringEncryptionKey = ConfigurationManager.AppSettings["ConnStringEncryptionKey"];
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                if (_connectionString == null)
                {
                    string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                    string conStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                    if (conStringEncrypt == "true")
                    {
                        connectionString = DataEncryption.Decrypt(connectionString, ConnStringEncryptionKey);
                    }
                    _connectionString = connectionString;
                    return connectionString;
                }
                return _connectionString;
            }
        }
        private static string _connectionString;
        /// <summary>
        /// 得到web.config里配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string conStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (conStringEncrypt == "true")
            {
                connectionString = DataEncryption.Decrypt(connectionString, ConnStringEncryptionKey);
            }
            return connectionString;
        }


    }
}
